package cn.maidouya.org.controller;

import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.org.domain.Shop;
import cn.maidouya.org.query.ShopQuery;
import cn.maidouya.org.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {
    @Autowired
    private ShopService shopService;

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
        }
    }
    @PatchMapping
    @ApiOperation(value = "批量删除店铺",notes = "批量删除店铺")
    public AjaxResult batchDel(@RequestBody Long[] ids) {
        System.err.println(Arrays.toString(ids));
        try {
        shopService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
        }
    }
    /**
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查找店铺",notes = "根据ID查找店铺")
    public Shop loadById(@PathVariable Long id) {
        Shop one = shopService.loadById(id);
        return one;
    }

    @GetMapping
    @ApiOperation(value = "查找全部店铺",notes = "查找所有店铺信息")
    public List<Shop> loadAll() {
        List<Shop> list = shopService.loadAll();
        return list;
    }
    @PostMapping
    @ApiOperation(value = "查找店铺分页数据",notes = "分页查找所有店铺信息")
    public PageList<Shop> queryData(@RequestBody ShopQuery shopQuery) {
        PageList<Shop> pageList = shopService.queryData(shopQuery);
        return pageList;
    }
    @PutMapping
    @ApiOperation(value = "添加和修改店铺",notes = "添加和修改店铺")
    public AjaxResult saveAndUpdate(@RequestBody Shop shop) {
                try {
                    if (shop.getId() == null){
                        shopService.save(shop);
                    } else {
                        shopService.update(shop);
                    }
                    return new AjaxResult();
                } catch (Exception e) {
                    e.printStackTrace();
                    return  AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
                }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除店铺",notes = "根据id删除店铺")
    public AjaxResult delete(@PathVariable Long id) {
        try {
          shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
    }
}